package com.tencent.wxcloudrun.project.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.wxcloudrun.common.Result;
import com.tencent.wxcloudrun.project.entity.Order;
import com.tencent.wxcloudrun.project.entity.OrderVariety;
import com.tencent.wxcloudrun.project.entity.Variety;
import com.tencent.wxcloudrun.project.entity.muti.OrderMuti;
import com.tencent.wxcloudrun.project.entity.muti.OrderVarietyMuti;
import com.tencent.wxcloudrun.project.entity.muti.VarietyMuti;
import com.tencent.wxcloudrun.project.entity.query.OrderQuery;
import com.tencent.wxcloudrun.project.entity.query.VarietyQuery;
import com.tencent.wxcloudrun.project.service.OrderService;
import com.tencent.wxcloudrun.project.service.OrderVarietyService;
import com.tencent.wxcloudrun.project.service.VarietyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

@Controller
@ResponseBody
@RequestMapping("/variety")
public class VarietyController {
    @Value("${files.upload.path}")
    private String fileUploadPath;
    @Autowired
    private VarietyService varietyService;
    @Autowired
    private OrderVarietyService orderVarietyService;

    @PostMapping("/addOrderVariety")
    public Result addOrderVariety(@RequestBody OrderVarietyMuti orderVarietyMuti){
        return Result.success(orderVarietyService.saveBatch(orderVarietyMuti.getOrderVarietyList()));
    }

    @GetMapping("/pageList")
    public Result pageList(@RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "8") int pageSize
    ){
        VarietyQuery varietyQuery=new VarietyQuery();
        varietyQuery.setName(name);
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<VarietyMuti> pageInfo = new PageInfo<>(varietyService.pageList(varietyQuery));
        return Result.success(pageInfo);
    }
    @GetMapping("/list")
    public Result list(){
        VarietyQuery varietyQuery=new VarietyQuery();
        varietyQuery.setIsShow(1);
        return Result.success(varietyService.pageList(varietyQuery));
    }
    @PostMapping("/update")
    public Result update(Long id, String name, Long classificationId, BigDecimal price, Long unitId, Integer isShow, Long normsType, MultipartFile img){
        String resourceUrl = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "static/files/";
//        String resourceUrl = fileUploadPath;
//           找到之前的文件
        Variety variety=new Variety();
        if(id!=null){
            Variety variety1 = varietyService.getById(id);
            if(variety1==null){
                return Result.error(401,"文件id错误");
            }
            variety.setId(id);
            if(img!=null){
                File tempFile=new File(variety1.getImg());
                //删除以前照片
                String realPath =  resourceUrl+tempFile.getName();
                File fileBefore = new File(realPath);
                //如果原来有图片则删除
                if (fileBefore.exists()) {
                    fileBefore.delete();
                }
            }
        }
        if(img!=null){
            String uuid= IdUtil.fastSimpleUUID();
            String fileUrl = "/files/" + uuid + img.getOriginalFilename();
            String filePath = resourceUrl + uuid + img.getOriginalFilename();
            File newFile=new File(filePath);
            File parentFile = newFile.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            try {
                FileUtil.writeBytes(img.getBytes(), filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            variety.setImg(fileUrl);
        }
        variety.setName(name);
        variety.setClassificationId(classificationId);
        variety.setPrice(price);
        variety.setUnitId(unitId);
        variety.setIsShow(isShow);
        variety.setNormsType(normsType);
        return Result.success(varietyService.saveOrUpdate(variety));
    }
}
